package com.tql.mall.merchant.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tql.mall.common.result.R;
import com.tql.mall.merchant.service.LogisInfoService;
import com.tql.mall.model.enums.OrderStatus;
import com.tql.mall.model.merchant.LogisInfo;
import com.tql.mall.order.client.OrderFeignClient;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/merchant/logis")
@Api("物流信息相关接口")
public class LogisInfoController {

    @Autowired
    private LogisInfoService logisInfoService;

    @Autowired
    private OrderFeignClient orderFeignClient;



    @PostMapping("updateStatus")
    public R<Void> updateStatus(@RequestBody LogisInfo logisInfo){

        //更新订单状态[order_info]
        orderFeignClient.updateOrderStatus(logisInfo.getOrderId(),1);//已发货
        //更新运单状态[logis_info]
        LogisInfo logisInfo1 = logisInfoService.getById(logisInfo.getId());
        logisInfo1.setOrderStatus(OrderStatus.DELIVERED.name());//已发货
        logisInfo1.setUpdateTime(new Date());
        logisInfoService.updateById(logisInfo1);
        return R.ok();
    }


    @GetMapping("{page}/{limit}")
    public R<Map<String,Object>> getLogisListPage(@PathVariable Integer page, @PathVariable Integer limit , Long shopId){

        IPage<LogisInfo> page1 = new Page<>(page,limit);
        QueryWrapper<LogisInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_id",shopId);
        logisInfoService.page(page1,queryWrapper);
        HashMap<String,Object> map = new HashMap<>();
        map.put("total",page1.getTotal());
        map.put("record",page1.getRecords());
        return R.ok(map);
    }
}
